﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;

namespace WuxiWTO.Web.backend
{
    public partial class userList : BasePage
    {
        WTODataContext contex = new WTODataContext();
        PagedDataSource psd = new PagedDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        private void BindGrid()
        {
            var list = from c in contex.supporters orderby c.update_date descending select c;
          
            var query = list.AsQueryable();
            if (this.txtLoginName.Text.Trim() != "")
            {
                query= list.Where(p => p.supporter_login_name.Contains(this.txtLoginName.Text.Trim()));
            }
            if (this.ddlValidate.SelectedItem.Value!="")
            {
                char activeFlag = char.Parse(this.ddlValidate.SelectedItem.Value);
                query = list.Where(p => p.is_active == activeFlag);
            }
            if (Session["admin_type"].ToString() != "1")//如果不是超级管理员，那么就取得对应注册目的的用户供审核
            {
                //query = list.Where(p => p.purpose == Session["admin_type"].ToString());
            }

            List<supporter> listSup = query.ToList();
            AspNetPager1.RecordCount = listSup.Count;
            psd.DataSource = listSup;
            psd.AllowPaging = true;
            psd.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            psd.PageSize = AspNetPager1.PageSize;

            this.rptUser.DataSource = psd;
            this.rptUser.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void rptUser_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var item = e.Item.DataItem;
                supporter cat = item as supporter;
                Label lblKind = e.Item.FindControl("lblKind") as Label;
                Label recordNo = e.Item.FindControl("lblNo") as Label;
                Label lvlValidateStatus = e.Item.FindControl("lblValidateStatus") as Label;
                switch (cat.is_active)
                {
                    case 'Y': lvlValidateStatus.Text = "审核通过"; lvlValidateStatus.ForeColor = System.Drawing.Color.Green; break;
                    case 'N': lvlValidateStatus.Text = "未审核"; lvlValidateStatus.ForeColor = System.Drawing.Color.Red; break;
                    case 'F':
                        if (!string.IsNullOrEmpty(cat.validate_reason))
                        {
                            lvlValidateStatus.Text = "审核驳回" + "<br/>(" + cat.validate_reason + ")";
                        }
                        else
                        {
                            lvlValidateStatus.Text = "审核驳回";
                        }
                        lvlValidateStatus.ForeColor = System.Drawing.Color.Red; break;
                }

                lblKind.Text = cat.supporter_type == 'I' ? "个人" : "企业";
                
                recordNo.Text = (e.Item.ItemIndex + 1 + (AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1))).ToString();
                
               //utton.Attributes.Add("onclick", "return confirm('您确定要删除该用户吗?');");
            }

        }

        protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DeleteCate")
            {
                int supporter_id = Convert.ToInt32(e.CommandArgument);
                supporter cat = contex.supporters.SingleOrDefault(p => p.supporter_id == supporter_id);
                if (cat != null)
                {
                    contex.supporters.DeleteOnSubmit(cat);
                    contex.SubmitChanges();
                }
                BindGrid();
            }
            if (e.CommandName == "Validate")
            {
                int supporter_id = Convert.ToInt32(e.CommandArgument);
                supporter cat = contex.supporters.SingleOrDefault(p => p.supporter_id == supporter_id);
                if (cat != null)
                {
                    cat.is_active = 'Y';
                    contex.SubmitChanges();
                }
                BindGrid();
            }
        }
    }
}